package xin.marcher.module.construction.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.module.common.constants.construction.ProductConstants;
import xin.marcher.module.common.enums.ProductTypeEnum;
import xin.marcher.module.common.enums.construction.ProductExceptionCode;
import xin.marcher.module.common.enums.seller.StoreLabelEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.construction.domain.entity.*;
import xin.marcher.module.construction.mapper.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 商品状态变更 资源管理
 */
@Repository
public class ProductStatusRepository {

    @Resource
    private FrontCategoryRelationMapper frontCategoryRelationMapper;

    @Resource
    private ItemInfoMapper itemInfoMapper;

    @Resource
    private ItemVideoImgMapper itemVideoImgMapper;

    @Resource
    private QualityControlMapper qualityControlMapper;

    @Resource
    private AttributeExtendMapper attributeExtendMapper;


    /**
     * 查询商品得详情信息
     *
     * @param itemId
     */
    public ProductDetailDO queryProductDetail(String itemId) {
        List<ProductDetailDO> productDetailDOList = itemInfoMapper.queryProductInfoList(itemId);
        if (CollectionUtils.isEmpty(productDetailDOList)) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        return productDetailDOList.get(0);
    }


    /**
     * 查询商品和前端类目的绑定关系
     *
     * @param itemId
     * @return
     */
    public List<FrontCategoryRelationDO> queryFrontCategoryList(String itemId) {
        LambdaQueryWrapper<FrontCategoryRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FrontCategoryRelationDO::getParticipateId, itemId);
        List<FrontCategoryRelationDO> frontCategoryRelationDOS = frontCategoryRelationMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(frontCategoryRelationDOS)) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_CATEGORY_RELATION_NULL);
        }
        return frontCategoryRelationDOS;
    }

    /**
     * 更新商品状态
     *
     * @param itemInfoDO       更新商品的对象信息
     * @param updateItemStatus 变更状态
     */
    public void updateProductStatus(ItemInfoDO itemInfoDO, Integer updateItemStatus) {
        LambdaUpdateWrapper<ItemInfoDO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ItemInfoDO::getItemId, itemInfoDO.getItemId());
        updateWrapper.eq(ItemInfoDO::getItemStatus, itemInfoDO.getItemStatus());
        updateWrapper.set(ItemInfoDO::getItemStatus, updateItemStatus);

        int count = itemInfoMapper.update(updateWrapper.getEntity(), updateWrapper);
        if (count <= 0) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_SQL);
        }
    }

    /**
     * 获取商品的扩展信息
     *
     * @param itemId
     */
    public AttributeExtendDO getAttributeExtend(String itemId) {
        LambdaUpdateWrapper<AttributeExtendDO> queryWrapper = Wrappers.lambdaUpdate();
        queryWrapper.eq(AttributeExtendDO::getParticipateId, itemId);
        queryWrapper.eq(AttributeExtendDO::getParticipateType, ProductTypeEnum.ITEM.getCode());

        return attributeExtendMapper.selectOne(queryWrapper);
    }

    /**
     * 更新商品的扩展信息
     *
     * @param productDetailDO
     */
    public void updateAttributeExtend(ProductDetailDO productDetailDO) {
        LambdaUpdateWrapper<AttributeExtendDO> updateWrapper = Wrappers.lambdaUpdate();

        updateWrapper.eq(AttributeExtendDO::getParticipateId, productDetailDO.getItemId());
        updateWrapper.eq(AttributeExtendDO::getParticipateType, ProductTypeEnum.ITEM.getCode());
        updateWrapper.set(AttributeExtendDO::getAttributeContent, productDetailDO.getAttributeContent());


        int count = attributeExtendMapper.update(updateWrapper.getEntity(), updateWrapper);
        if (count <= 0) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_SQL);
        }
    }

    /**
     * 验证商品得完整性
     *
     * @param productDetailDO
     */
    public void checkProductComplete(ProductDetailDO productDetailDO) {
        //1. 验证商品的存储信息是否完整 
        if (Objects.isNull(productDetailDO.getStoreConditionType())
                || Objects.isNull(StoreLabelEnum.getByCode(productDetailDO.getStoreConditionType()))) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        //2. 是否有推广信息
        if (StringUtils.isBlank(productDetailDO.getRecommend())) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        //3.是否有销售信息
        if (Objects.isNull(productDetailDO.getBasePrice()) || Objects.isNull(productDetailDO.getVipPrice())) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        //4.验证商品是否品控信息完整
        List<QualityControlDO> qualityControlList = getQualityControlList(productDetailDO.getItemId());
        if (CollectionUtils.isEmpty(qualityControlList)) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        //5.是否有图文信息(必须包含1-主图，2-轮播图、3-详情图)
        List<ItemVideoImgDO> itemVideoImgList = getItemVideoImgList(productDetailDO.getItemId());
        // 先过滤掉不需要的图文信息
        List<ItemVideoImgDO> videoImgDOList = itemVideoImgList.stream().filter(itemVideoImgDO -> {
            if (ProductConstants.PRODUCT_IMG_TYPE_LIST.contains(itemVideoImgDO.getContentType())) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        // 空验证
        if (CollectionUtils.isEmpty(itemVideoImgList)) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
        // 获取图文集合类型
        Set<Integer> contentTypeList = videoImgDOList.stream().map(ItemVideoImgDO::getContentType).collect(Collectors.toSet());
        // 过滤掉不符合的图片类型后，去重后，剩余的图片类型要和厂里存储的一样多
        if (contentTypeList.size() != ProductConstants.PRODUCT_IMG_TYPE_LIST.size()) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_DETAIL_NULL);
        }
    }


    /**
     * 获取商品的质检报告详情
     *
     * @param itemId
     * @return
     */
    private List<QualityControlDO> getQualityControlList(String itemId) {
        LambdaQueryWrapper<QualityControlDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(QualityControlDO::getItemId, itemId);
        return qualityControlMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品的某种图文信息
     *
     * @param itemId
     * @return
     */
    private List<ItemVideoImgDO> getItemVideoImgList(String itemId) {
        LambdaQueryWrapper<ItemVideoImgDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemVideoImgDO::getItemId, itemId);

        return itemVideoImgMapper.selectList(queryWrapper);
    }
}
